package LeetCode刷题.暑假刷的算法;

/**
 * @program: Java_Study
 * @author: Xiaofan
 * @createTime: 2021-07-28 16:35
 * @description: Functions of this class is 判断两棵树是否相等
 **/
public class 判断两棵树是否相等 {
    private static class TNode{
        TNode left,right;
        Integer val;
    }
    public static void main(String args[]){
        TNode T1=createTree1();
        TNode T2=createTree2();
        if (judge(T1, T2)) {
            System.out.println("这两棵树是相等的!");
        } else {
            System.out.println("这两棵树不相等!");
        }
    }

    private static boolean judge(TNode t1, TNode t2) {
        if(t1!=null&&t2!=null){
            if(!t1.val.equals(t2.val)){
                return false;
            }
            return judge(t1.left,t2.left)&&judge(t1.right,t2.right);
        }
        return t1==null&&t2==null?true:false;
    }

    private static TNode createTree1() {
        TNode root = new TNode();

        TNode node1 = new TNode();
        TNode node2 = new TNode();
        TNode node3 = new TNode();
        TNode node4 = new TNode();
        /*
                6
             3    -7
          -1  9
         */
        root.val = 6;
        node1.val = 3;
        node2.val = -7;
        node3.val = -1;
        node4.val = 9;
        root.left = node1;
        root.right = node2;
        node1.left = node3;
        node1.right = node4;
        node2.left = node2.right = node3.left = node3.right = node4.left = node4.right = null;
        return root;
    }
    private static TNode createTree2() {
        TNode root = new TNode();

        TNode node1 = new TNode();
        TNode node2 = new TNode();
        TNode node3 = new TNode();
        TNode node4 = new TNode();
        /*
                6
             3    -7
          -1  9
         */
        root.val = 6;
        node1.val = 3;
        node2.val = -7;
        node3.val = -1;
        node4.val = 9;
        root.left = node1;
        root.right = node2;
        node1.left = node3;
        node1.right = node4;
        node2.left = node2.right = node3.left = node3.right = node4.left = node4.right = null;
        return root;
    }
}